<?php

App::uses('AppModel', 'Model');

/**
 * Menu Model
 * 
 * PHP versions 5
 * CAKEPHP versions 2.x
 * 
 * Green CMS - Content Management System and Framework Powerfull by Cakephp
 * Copyright 2012, GREEN GLOBAL CO., LTD (toancauxanh.vn)
 * 
 * CakePHP(tm) :  Rapid Development Framework (http://www.cakephp.org)
 * Copyright 2005-2011, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @author        Technology Lab No.I <tech1@toancauxanh.vn>
 * @link          
 * @package       Green.Model
 * @since         Green v 1.0
 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 *
 * @property Block $Block
 * @property MenuLink $MenuLink
 */
class Menu extends AppModel {

    /**
     * List of behaviors
     *
     * @var array
     */
    public $actsAs = array(
        'Containable'
    );

    /**
     * Defined to load the config file stored.
     *
     * @var string
     */
    public $configKey = 'Menu';

    /**
     * Validation rules.
     *
     * @var array
     */
    public $validate = array(
        'name' => array(
            'required' => array(
                'rule' => '//',
                'on' => 'create',
                'required' => true,
                'message' => 'This field is not blank!',
            ),
            'maxlength' => array(
                'allowEmpty' => false,
                'rule' => array('maxLength', 26),
                'message' => 'Menu name must be no larger than 26 characters long.',
            )
        )
    );

    /**
     * hasMany associations.
     *
     * @var array
     */
    public $hasMany = array(
        'MenuLink' => array(
            'className' => 'Menu.MenuLink',
            'foreignKey' => 'menu_id',
            'dependent' => true
        )
    );

    /**
     * hasOne associations.
     *
     * @var array
     */
    public $hasOne = array(
        'Block' => array(
            'className' => 'Block.Block',
            'foreignKey' => 'delta',
            'conditions' => array('Block.module' => 'Menu'),
            'dependent' => true
        )
    );

    /**
     * Create menu id
     *
     * Create a new block
     *
     * @return boolean
     */
    public function beforeSave($options = array()) {
        if (empty($this->id) && empty($this->data[$this->alias][$this->primaryKey])) {
            $this->data[$this->alias][$this->primaryKey] = $this->getUnique($this->primaryKey, $this->data[$this->alias]['name'], '-');
        }
        return parent::beforeSave($options);
    }

    /**
     * after save callback
     *
     * Create a new block
     *
     * @return boolean
     */
    public function afterSave($created) {
        if (empty($created)) {
            return true;
        }
        $data = array(
            'description' => $this->data[$this->alias]['name'],
            'module' => 'Menu',
            'delta' => $this->getInsertID(),
            'custom' => 0
        );
        $this->Block->create();
        $this->Block->save($data, false);
    }

    /**
     * After delete
     *
     * Delete menu block
     *
     * @return boolean

      function afterDelete() {
      $this->Block->deleteAll(
      array(
      'Block.delta' => $this->id,
      'Block.module' => 'Menu'
      )
      );
      }
     */
}

?>